<?php


namespace App\Traits;


use App\Jobs\LogJob;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Http\Request;
use LAuth\Models\User;

trait Logs
{
    /**
     * @param $model
     * @param string $remark
     * @param array $extra
     * @author luffyzhao@vip.126.com
     */
    protected function recordLogs($model, string $remark, array $extra = [])
    {
        if(method_exists($model, 'logs') && $model->logs() instanceof HasMany){
            $model->logs()->create(array_merge($extra, [
                'remark' => $remark,
                'ip' => auth()->user() ? Request::createFromGlobals()->getClientIp() : '0.0.0.0',
                'foreign' => User::class,
                'foreign_key' => auth()->user() ? auth()->user()->getKey() : 0
            ]));
        }
    }
}
